//
//  WBVisitorView.swift
//  WeiBo
//
//  Created by chenguanglei on 16/11/26.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

protocol WBVisitorViewDelegate:NSObjectProtocol {
    
    
    func registerButtonClick()
    func loginButtonClick()
    
}


private let VisitorMargin = 10

class WBVisitorView: UIView {

    
    weak var delegate: WBVisitorViewDelegate?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
    
        setupUI()
        
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        setupUI()
    }
    
    
    
    func setupVisitor(title:String,imageNamge:String,isHidden:Bool=true) {
        
        animationImage.isHidden = isHidden
        
        iconImageView.image = UIImage(named:imageNamge)
        
        messageLabel.text = title
        
        if isHidden==false {
            
            animation()
        }
        
    }
    
    
    
    
    //设置界面
    private func setupUI (){
    
    
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        addSubview(animationImage)
        
        
        animationImage.snp_makeConstraints { (make) in
            make.center.equalTo(self.center)
        }
        
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self.center)
        }
        
        
        messageLabel.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(animationImage.snp_bottom).offset(VisitorMargin)
            make.width.equalTo(220)
        }
        
        
        registerButton.snp_makeConstraints { (make) in
            
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(VisitorMargin)
            
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        
        
        loginButton.snp_makeConstraints { (make) in
            
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(VisitorMargin)
            
            make.width.equalTo(100)
            make.height.equalTo(35)

            
        }
        
        registerButton.addTarget(self, action: #selector(registerButtonClick), for: .touchUpInside)
        loginButton.addTarget(self, action: #selector(loginButtonClick), for: .touchUpInside)
    
    }
    
    
    
    @objc private func registerButtonClick(){
    
        delegate?.registerButtonClick()
    
    }
    
    
    @objc private func loginButtonClick(){
    
        delegate?.loginButtonClick()
    
    }
    
    
    
    //首页动画
    
    private func animation(){
    
        let anim = CABasicAnimation()
        
        anim.keyPath = "transform.rotation"
        
        anim.toValue = M_PI * 2
        
        anim.duration = 20
        
        anim.repeatCount = MAXFLOAT
        
        anim.isRemovedOnCompletion = false
        
        animationImage.layer.add(anim, forKey: nil)
    
    }
    
    
    
    //懒加载添加控件
    private lazy var iconImageView : UIImageView = UIImageView(image:UIImage(named:"visitordiscover_image_message"))
    
    
    private lazy var messageLabel : UILabel = {
    
        let label = UILabel()
    
        label.text = "登陆后,别人评论的你的微博,发给你的消息,都会在这里收到通知"
        
        label.textColor = UIColor.lightGray
        
        label.font = UIFont.systemFont(ofSize: 14)
        
        label.numberOfLines = 0
        
        label.textAlignment = NSTextAlignment.center
    
        return label
    }()
    
    
    private lazy var registerButton : UIButton = {
    
        let button = UIButton(titleNormal: "注册", titleColor: UIColor.darkGray, size: 14, bgimageNormal: "common_button_white_disable", imageNormal: nil, corner: nil)
    
        return button
    }()
    
    private lazy var loginButton : UIButton = {
    
       let button = UIButton(titleNormal: "登陆", titleColor: UIColor.darkGray, size: 14, bgimageNormal: "common_button_white_disable", imageNormal: nil, corner: 5)
    
        return button
    }()
    
    
    private lazy var animationImage : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    

}
